package com.ws.zc.web.auth;

import com.ws.rain.base.utils.encrypt.Base64Util;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username =  principal.getUsername();
        String url = response.encodeURL("http://localhost:8080/user/success?username="+Base64Util.encode(username));
        response.sendRedirect(url);

    }
}
